package com.gxpt.app.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;

import com.gxpt.app.AppUtils;
import com.gxpt.app.Constant;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.MyApp;
import com.gxpt.app.R;
import com.gxpt.app.fragment.Fragment_center;
import com.gxpt.app.fragment.Fragment_home;
import com.gxpt.app.fragment.Fragment_mall;
import com.gxpt.app.fragment.Fragment_shopping;
import com.utils.T;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dufeng on 2017/9/12.
 */

@ContentView(R.layout.activity_home)
public class Activity_home extends FragmentActivity {


    @ViewInject(R.id.fragment_content)
    FrameLayout fragment_content;

    @ViewInject(R.id.radioButton0)
    RadioButton radioButton0;

    @ViewInject(R.id.radioButton1)
    RadioButton radioButton1;

    @ViewInject(R.id.radioButton2)
    RadioButton radioButton2;

    @ViewInject(R.id.radioButton3)
    RadioButton radioButton3;

    Context                           context;
    Fragment                          fragment_home;
    Fragment                          fragment_mall;
    Fragment                          fragment_shopping;
    Fragment                          fragment_center;
    Fragment                          currentFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    int index = 0 ;
    private static final  String[] FRAGMENT_TAG = {"home","mall","shopping","center"};
    List<Fragment> fragments = new ArrayList<Fragment>();

    Drawable myImage0 = null;
    Drawable myImage1 = null;
    Drawable myImage2 = null;
    Drawable myImage3 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        x.view().inject(this);
        context = this;
        MyApp.getInstance().addActivity(this);



        /**
         * 拿到事务管理器并开启事务
         */
        manager = getSupportFragmentManager();
        initViews();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){
        switch (messageEvent.getMessageType()){
            case Constant.ACTION_FRAGMENT_TO_HOME:
//                T.show(context,"dfdfff",1);
                index = 0 ;
                changeMenuView();
                switchContent(fragment_home);
                break;
        }
    }



    private void initViews() {
        fragment_home   = new Fragment_home();
        fragments.add(0,fragment_home);
        fragment_mall = new Fragment_mall();
        fragments.add(1,fragment_mall);
        fragment_shopping  = new Fragment_shopping();
        fragments.add(2,fragment_shopping);
        fragment_center  = new Fragment_center();
        fragments.add(3,fragment_center);
        setDefaultFragment();
    }

    private void setDefaultFragment() {
        transaction = manager.beginTransaction();
        transaction.add(R.id.fragment_content, fragment_home,FRAGMENT_TAG[index]).commit();
        currentFragment = fragment_home;
    }

    /**
     * 修改显示的内容 不会重新加载 *
     */
    public void switchContent(Fragment to) {
        if (!currentFragment.getTag().equals(to.getTag())) {

            transaction = manager.beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(currentFragment).add(R.id.fragment_content, to,FRAGMENT_TAG[index]).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(currentFragment).show(to).commit(); // 隐藏当前的fragment，显示下一个

            }
            currentFragment = to;
        }
    }

    /**
     * 修改显示的内容 但会重新加载 *
     */
    public void switchContent_reload(Fragment to){
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fragment_content,to);
        //transaction.addToBackStack(null);
        transaction.commit();
    }

    public void changeMenuView(){
        switch(index){
            case 0:
                myImage0 = null;
                myImage0 = getResources().getDrawable(R.mipmap.menu0_sel);
                radioButton0.setCompoundDrawablesWithIntrinsicBounds(null, myImage0, null, null);
                radioButton0.setTextColor(getResources().getColor(R.color.red));

                myImage1 = null;
                myImage1 = getResources().getDrawable(R.mipmap.menu1);
                radioButton1.setCompoundDrawablesWithIntrinsicBounds(null, myImage1, null, null);
                radioButton1.setTextColor(getResources().getColor(R.color.font_686868));

                myImage2 = null;
                myImage2 = getResources().getDrawable(R.mipmap.menu2);
                radioButton2.setCompoundDrawablesWithIntrinsicBounds(null, myImage2, null, null);
                radioButton2.setTextColor(getResources().getColor(R.color.font_686868));

                myImage3 = null;
                myImage3 = getResources().getDrawable(R.mipmap.menu3);
                radioButton3.setCompoundDrawablesWithIntrinsicBounds(null, myImage3, null, null);
                radioButton3.setTextColor(getResources().getColor(R.color.font_686868));

                break;
            case 1:
                myImage0 = null;
                myImage0 = getResources().getDrawable(R.mipmap.menu0);
                radioButton0.setCompoundDrawablesWithIntrinsicBounds(null, myImage0, null, null);
                radioButton0.setTextColor(getResources().getColor(R.color.font_686868));

                myImage1 = null;
                myImage1 = getResources().getDrawable(R.mipmap.menu1_sel);
                radioButton1.setCompoundDrawablesWithIntrinsicBounds(null, myImage1, null, null);
                radioButton1.setTextColor(getResources().getColor(R.color.red));

                myImage2 = null;
                myImage2 = getResources().getDrawable(R.mipmap.menu2);
                radioButton2.setCompoundDrawablesWithIntrinsicBounds(null, myImage2, null, null);
                radioButton2.setTextColor(getResources().getColor(R.color.font_686868));

                myImage3 = null;
                myImage3 = getResources().getDrawable(R.mipmap.menu3);
                radioButton3.setCompoundDrawablesWithIntrinsicBounds(null, myImage3, null, null);
                radioButton3.setTextColor(getResources().getColor(R.color.font_686868));
                break;
            case 2:
                myImage0 = null;
                myImage0 = getResources().getDrawable(R.mipmap.menu0);
                radioButton0.setCompoundDrawablesWithIntrinsicBounds(null, myImage0, null, null);
                radioButton0.setTextColor(getResources().getColor(R.color.font_686868));

                myImage1 = null;
                myImage1 = getResources().getDrawable(R.mipmap.menu1);
                radioButton1.setCompoundDrawablesWithIntrinsicBounds(null, myImage1, null, null);
                radioButton1.setTextColor(getResources().getColor(R.color.font_686868));

                myImage2 = null;
                myImage2 = getResources().getDrawable(R.mipmap.menu2_sel);
                radioButton2.setCompoundDrawablesWithIntrinsicBounds(null, myImage2, null, null);
                radioButton2.setTextColor(getResources().getColor(R.color.red));

                myImage3 = null;
                myImage3 = getResources().getDrawable(R.mipmap.menu3);
                radioButton3.setCompoundDrawablesWithIntrinsicBounds(null, myImage3, null, null);
                radioButton3.setTextColor(getResources().getColor(R.color.font_686868));
                break;
            case 3:
                myImage0 = null;
                myImage0 = getResources().getDrawable(R.mipmap.menu0);
                radioButton0.setCompoundDrawablesWithIntrinsicBounds(null, myImage0, null, null);
                radioButton0.setTextColor(getResources().getColor(R.color.font_686868));

                myImage1 = null;
                myImage1 = getResources().getDrawable(R.mipmap.menu1);
                radioButton1.setCompoundDrawablesWithIntrinsicBounds(null, myImage1, null, null);
                radioButton1.setTextColor(getResources().getColor(R.color.font_686868));

                myImage2 = null;
                myImage2 = getResources().getDrawable(R.mipmap.menu2);
                radioButton2.setCompoundDrawablesWithIntrinsicBounds(null, myImage2, null, null);
                radioButton2.setTextColor(getResources().getColor(R.color.font_686868));

                myImage3 = null;
                myImage3 = getResources().getDrawable(R.mipmap.menu3_sel);
                radioButton3.setCompoundDrawablesWithIntrinsicBounds(null, myImage3, null, null);
                radioButton3.setTextColor(getResources().getColor(R.color.red));
                break;
        }
    }

    @Event(value={R.id.radioButton0,R.id.radioButton1,R.id.radioButton2,R.id.radioButton3})
    private void getEvent(View view){
         switch(view.getId()){

            case R.id.radioButton0:
                index = 0 ;
                changeMenuView();
                switchContent(fragment_home);
                break;
            case R.id.radioButton1:
                index = 1 ;
                changeMenuView();
                switchContent(fragment_mall);
                break;
            case R.id.radioButton2:
                if(AppUtils.get_user_id(context)>0){
                    index = 2 ;
                    changeMenuView();
                    switchContent(fragment_shopping);
                }else {
                    Intent go = new Intent();
                    go.setClass(context,Activity_login.class);
                    startActivity(go);
                }



//                Intent go1 = new Intent();
//                go1.setClass(context,Activity_shopping.class);
//                startActivity(go1);
                break;
            case R.id.radioButton3:
                if(AppUtils.get_user_id(context)>0){
                    index = 3 ;
                    changeMenuView();
                    switchContent(fragment_center);
                }else {
                    Intent go = new Intent();
                    go.setClass(context,Activity_login.class);
                    startActivity(go);
                }

                break;
        }
    }

    private long firstClickBack = 0;
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if(keyCode == KeyEvent.KEYCODE_BACK){
//            long secondClickBack = System.currentTimeMillis();
//            if(secondClickBack - firstClickBack >1500){
//                T.show(this, "再按一次退出", 0);
//                firstClickBack = secondClickBack;
//                return true;
//            }else{
//                MyApp.getInstance().exitApp();
//                return true;
//            }
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - firstClickBack) > 2000) {
                //弹出提示，可以有多种方式
                T.show(this, "再按一次退出程序", 0);
                firstClickBack = System.currentTimeMillis();
            } else {
                MyApp.getInstance().exitApp();
//                finish();
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
